Page({
  data: {
    addressList: []
  },

  onShow() {
    this.loadAddressList();
  },

  // 加载地址列表
  async loadAddressList() {
    try {
      // TODO: 调用获取地址列表接口
      // const res = await getAddressList();
      const res = {
        data: []
      };
      
      this.setData({
        addressList: res.data
      });
    } catch (error) {
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    }
  },

  // 添加新地址
  addAddress() {
    wx.navigateTo({
      url: '/pages/address/edit/index'
    });
  },

  // 编辑地址
  editAddress(event) {
    const { id } = event.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/address/edit/index?id=${id}`
    });
  },

  // 删除地址
  deleteAddress(event) {
    const { id } = event.currentTarget.dataset;
    wx.showModal({
      title: '提示',
      content: '确定要删除该地址吗？',
      success: async (res) => {
        if (res.confirm) {
          try {
            // TODO: 调用删除地址接口
            // await deleteAddress(id);
            wx.showToast({
              title: '删除成功',
              icon: 'success'
            });
            this.loadAddressList();
          } catch (error) {
            wx.showToast({
              title: '删除失败',
              icon: 'none'
            });
          }
        }
      }
    });
  },

  // 设置默认地址
  async setDefaultAddress(event) {
    const { id } = event.currentTarget.dataset;
    try {
      // TODO: 调用设置默认地址接口
      // await setDefaultAddress(id);
      wx.showToast({
        title: '设置成功',
        icon: 'success'
      });
      this.loadAddressList();
    } catch (error) {
      wx.showToast({
        title: '设置失败',
        icon: 'none'
      });
    }
  }
}); 